﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Diagnostics;

namespace ReclutamientoCosea
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        string strOpcionActiva = "";

        #region "Constructor"

        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        #endregion

        #region "Métodos"

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            //objLogin.SeCerroVentana += new CerroVentana(objLogin_SeCerroVentana);
            objLogin.SeCerroVentana += new VentanaLogin(objLogin_SeCerroVentana);
            PanelAcciones.Visibility = Visibility.Collapsed;
            oCand.SeCerroVentana += new CerroVentana(oCand_SeCerroVentana);
            objetoListaCandidatos.SeCerroVentana += new CerroVentana(objetoListaCandidatos_SeCerroVentana);
            oFormatoOC.SeCerroVentana += new CerroVentana(oFormatoOC_SeCerroVentana);
            usrNavegadorWeb1.SeCerroVentana += new CerroVentana(usrNavegadorWeb1_SeCerroVentana);
            oAdmOC.SeCerroVentana += new CerroVentana(oAdmOC_SeCerroVentana);
            string version = muestraVersionClickOnce();
            if (version == "")
            {
                version = muestraVersionNormal();
            }
            this.Title = "Reclutamiento Cosea versión " + version;
            lblVersion.Content = "Sistema Reclutamiento ver." + version;

            string directorioReportes = "c:\\tmpReportes";
            if (!System.IO.Directory.Exists(@directorioReportes))            
                System.IO.Directory.CreateDirectory(@directorioReportes);
            

        }

        void oAdmOC_SeCerroVentana(bool cerro)
        {
            PanelAcciones.Visibility = Visibility.Collapsed;
            OCAdm.Visibility = Visibility.Collapsed;
            strOpcionActiva = "Princpal";
            PaneMain.Visibility = Visibility.Visible;
        }

        private string muestraVersionClickOnce()
        {
            Version myVersion;
            string resultado = "";
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                myVersion = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;

                resultado = myVersion.Major.ToString() + "." + myVersion.MajorRevision.ToString()
                + "." + myVersion.Revision.ToString();
            }
            return resultado;
        }

        private string muestraVersionNormal()
        {            
            string resultado = "";
            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            System.Reflection.AssemblyName an = a.GetName();
            Version v = an.Version;
            resultado = v.Major.ToString() + "." + v.MajorRevision.ToString()
                + "." + v.Revision.ToString();
            return resultado;
        }

        void objLogin_SeCerroVentana(bool exito, bool cancelar)
        {
            if (cancelar)
            {
                this.Close();
            }

            if (exito && cancelar == false )
            {
                 objLogin.Visibility = Visibility.Collapsed;                 
            }

        }

        void usrNavegadorWeb1_SeCerroVentana(bool cerro)
        {
            PanelAcciones.Visibility = Visibility.Collapsed;
            OLstCand.Visibility = Visibility.Collapsed;
            ONavegador.Visibility = Visibility.Collapsed;
            strOpcionActiva = "Princpal";
            PaneMain.Visibility = Visibility.Visible;
        }

        void oFormatoOC_SeCerroVentana(bool cerro)
        {
            PanelAcciones.Visibility = Visibility.Collapsed;
            OCdtll.Visibility = Visibility.Collapsed;            
            strOpcionActiva = "Princpal";
            PaneMain.Visibility = Visibility.Visible;
        }

        void objetoListaCandidatos_SeCerroVentana(bool cerro)
        {
            PanelAcciones.Visibility = Visibility.Collapsed;
            OLstCand.Visibility = Visibility.Collapsed;
            strOpcionActiva = "Princpal";
            PaneMain.Visibility = Visibility.Visible;
        }

        void oCand_SeCerroVentana(bool cerro)
        {
            PanelAcciones.Visibility = Visibility.Collapsed;            
            OCandidato.Visibility = Visibility.Collapsed;            
            strOpcionActiva = "Princpal";
        }

        //void objLogin_SeCerroVentana(bool cerro)
        //{
        //    objLogin.Visibility = Visibility.Collapsed;
        //}

        private void btnLogon_Click(object sender, RoutedEventArgs e)
        {
            objLogin.Visibility = Visibility.Visible;
        }

        private void vAdmOC_Selected(object sender, RoutedEventArgs e)
        {
            ejecutaOpcionMnu("vAdmOC");
        }

        private void ejecutaOpcionMnu(string opcion)
        {
            if (LogInLogOut.Usuario.idusuario != null)
            {
                try
                {
                    PaneMain.Visibility = Visibility.Collapsed;
                    switch (opcion)
                    {
                        case "vAdmOC":
                            strOpcionActiva = "vAdmonOC";
                            OCAdm.Visibility = Visibility.Visible;
                            oAdmOC.CargaOrdenTrabajo();
                            break;
                        case "vAgregarOC":
                            strOpcionActiva = "vAgregarOC";
                            //PanelAcciones.Visibility = Visibility.Visible;
                            OCdtll.Visibility = Visibility.Visible;
                            break;
                        case "vRegistraCand":
                            strOpcionActiva = "vRegistraCand";
                            ///PanelAcciones.Visibility = Visibility.Visible;
                            OCandidato.Visibility = Visibility.Visible;
                            oCand.LimpiaCajas();
                            break;
                        case "vBusquedaCand":
                            strOpcionActiva = "vBusquedaCand";
                            //PanelAcciones.Visibility = Visibility.Visible;
                            OLstCand.Visibility = Visibility.Visible;
                            break;
                        case "vCoseaNet":
                            strOpcionActiva = "vCoseaNet";
                            //PanelAcciones.Visibility = Visibility.Visible;
                            ONavegador.Visibility = Visibility.Visible;                            
                            break;
                    }

                }
                catch (Exception ex)
                {
                    wExcepcionesSistema wError = new wExcepcionesSistema();
                    wError.txtExcepcion.Text = ex.Message;
                    wError.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Si no estas firmado en el sistema no puedes entrar a las opciones",
                    "Reclutamiento Cosea",  MessageBoxButton.OK ,  MessageBoxImage.Stop );
            }
        }

        private void vAgregarOC_Selected(object sender, RoutedEventArgs e)
        {
            ejecutaOpcionMnu("vAgregarOC");
            
        }

        #endregion

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //switch (strOpcionActiva)
                //{
                    //case "vAgregarOC":                        
                    //    oFormatoOC.txtOC.Text =   AdmOT.GuardaOT(oFormatoOC.RecuperaDatos()).ToString ();
                    //    break;
                    //case "vRegistraCand":
                    //    oCand.RecuperaDatos ();
                    //    DataSet ds =   Candidatos.GuardaCandidato (oCand.Candto,oCand.Persna );
                    //    string idpersona = ds.Tables[1].Rows[0]["idpersona"].ToString();
                    //    string idcandidato = ds.Tables[1].Rows[0]["idcandidato"].ToString();       
                    //    Personas per = new Personas ();
                    //    oCand.Persna = per.GetPersona(idpersona);
                    //    oCand.Candto = Candidatos.GetCandidato(idcandidato);
                    //    per = null;
                    //    break;
                //}
                
            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
        }

        private void btnSalir_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                switch (strOpcionActiva)
                {
                    case "vAgregarOC":                    
                    case "vBusquedaCand":
                        PanelAcciones.Visibility = Visibility.Collapsed;
                        OCdtll.Visibility = Visibility.Collapsed;
                        OCandidato.Visibility = Visibility.Collapsed;
                        OLstCand.Visibility = Visibility.Collapsed;
                        strOpcionActiva = "Princpal";
                        PaneMain.Visibility = Visibility.Visible;
                        break;
                }                
            }
            catch (Exception ex)
            {

                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }

        }

        private void btnLimpiar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //switch (strOpcionActiva)
                //{
                    //case "vAgregarOC":
                    //    oFormatoOC.LimpiaCajas();
                    //    oFormatoOC.CargaCombos();
                    //    break;
                    //case "vRegistraCand":
                    //    oCand.LimpiaCajas();
                    //    break;
                //}
            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }

        }

        private void vRegistroCand_Selected(object sender, RoutedEventArgs e)
        {
            ejecutaOpcionMnu("vRegistraCand");
        }

        private void vBusquedaCand_Selected(object sender, RoutedEventArgs e)
        {
            ejecutaOpcionMnu("vBusquedaCand");
        }

        private void vSalir_Selected(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void vCoseaNet_Selected(object sender, RoutedEventArgs e)
        {
            ejecutaOpcionMnu("vCoseaNet");
        }
        
    }
}
